import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class CommonWebPage extends StatefulWidget {
  final String title;
  final String url;

  CommonWebPage({Key key, this.title, this.url})
      : assert(url != null),
        super(key: key);

  @override
  _CommonWebPageState createState() => _CommonWebPageState();
}

class _CommonWebPageState extends State<CommonWebPage> {
  FlutterWebviewPlugin _flutterWebViewPlugin = FlutterWebviewPlugin();
  double loadProgress;

  @override
  void initState() {
    super.initState();

    _flutterWebViewPlugin.onProgressChanged.listen((progress) {
      print(progress);
      setState(() {
        loadProgress = progress;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return WebviewScaffold(
      url: widget.url,
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          widget.title == null ? "" : widget.title,
          style: TextStyle(
            color: Colors.white,
          ),
        ),
        iconTheme: IconThemeData(color: Colors.white),
        //0412 added
        elevation: 10,
        actions: <Widget>[
          Icon(Icons.share),
          SizedBox(
            width: 10,
          ),
        ],
        leading: InkWell(
          child: Icon(Icons.close),
          onTap: () => {Navigator.pop(context)},
        ),
        backgroundColor: Colors.red,
        bottom: PreferredSize(
          child: _progressBar(loadProgress, context),
          preferredSize: Size.fromHeight(3),
        ),
      ),
      withJavascript: true,
      //允许执行js
      withLocalStorage: true,
      //允许本地存储
      withZoom: true, //允许网页缩放
    );
  }

  _progressBar(double progress, BuildContext context) {
    return SizedBox(
      height: 3,
      child: LinearProgressIndicator(
        backgroundColor: progress == 1 ? Colors.white70.withOpacity(0) : Colors.white70.withOpacity(0.5),
        value: progress == 1 ? 0 : progress,
        valueColor: new AlwaysStoppedAnimation<Color>(Colors.redAccent),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _flutterWebViewPlugin.dispose();
  }
}
